<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" name="password" />
    <input type="text" name="confirm_password" />
    <br />
    <span name="msg"></span>
  </body>
  <script>
    function query(name) {
      return document.querySelector(`[name=${name}]`);
    }

    let inputs = document.querySelectorAll(
      "[name='password'],[name='confirm_password']"
    );
    let span = query("msg");

    // [...inputs].forEach(item => {
    //   item.addEventListener("keyup", function(){
    //     let msg = "";
    //     if (query("password").value != query("confirm_password").value||query("password").value.length<5) {
    //       msg = "两次密码不一致,密码长度小于5";
    //     }
    //     span.innerHTML = msg;
    //   });
    // });

    [...inputs].forEach(item => {
      item.addEventListener("keyup", function() {
        span.innerHTML = (
          query("password").value != query("confirm_password").value ||
          query("password").value.length < 5
        )? "两次密码不一致,密码长度小于5" : "" ;
      });
    });
  </script>
</html>
